<template>
	<!--pages/store/home/home.wxml-->
	<view class="container">
		<!-- banner -->
		<view class="margin" @click="toStoreDetail">
			<view class="banner">
				<image :src="storeInfo.store_pic" mode="aspectFill"></image>
			</view>
		</view>

		<!-- 店铺名称 -->
		<view class="row store-name bottom-line margin">
			<text class="name" @click="toStoreDetail">{{storeInfo.store_name}}</text>
			<view class="row collect auto-left" @click.stop="collect">
				<image src="/static/imgs/collect-active.png" v-if="storeInfo.my_is_col==1"></image>
				<image src="/static/imgs/collect.png" v-else> </image>
				<text>喜欢这家店</text>
			</view>
		</view>
		<!-- 营业时间 -->
		<view class="col opening bottom-line margin">
			<view class="row header">
				<image :src="url+'/static/imgs/store.png'"></image>
				<view class="text">
					<text>{{storeInfo.status==1?'营业中':'休息中'}}</text>
					<text v-if="storeInfo.start_time"> | {{storeInfo.start_time}}-{{storeInfo.end_time}}</text>
				</view>
			</view>
			<view class="row tags">
				<text v-for="(item,index) in storeInfo.tags" v-bind:key="index">{{item}}</text>
			</view>
		</view>
		<!-- 位置 -->
		<view class="row address bottom-line margin">
			<view class="col address-info">
				<text class="text-ellipsis">{{storeInfo.map_address}}</text>
			</view>
			<view class="icon-group row">
				<image :src="url+'/static/imgs/loaction.png'" class="loaction" @click="openLocation"></image>
				<image :src="url+'/static/imgs/tel.png'" class="tel" @click="makePhoneCall"></image>
			</view>
		</view>
		<!-- 店面描述 -->
		<view class="row description bottom-line margin">
			<view class="row item" @click="toCategory">
				<image :src="url+'/static/imgs/icon/order.png'"></image>
				<text>在家点餐 到店自取</text>
			</view>

			<view class="row item" @click="toPlayTour" v-bind:id="storeInfo.id">
				<image :src="url+'/static/imgs/icon/shang.png'"></image>
				<text>打赏全场 即可参加</text>
			</view>
		</view>

		<!-- 优惠券 -->
		<view class="col coupon margin">
			<view class="col">
				<view class="row title">
					<view class="row">
						<image :src="url+'/static/imgs/icon/quan.png'" class="icon"></image>
						<text>优惠券</text>
					</view>
					<view class="row more auto-left" @click="toCouponList">
						<text>更多({{couponInfo.coupon_num||0}})</text>
						<image :src="url+'/static/imgs/more-gray.png'"></image>
					</view>
				</view>
				<view class="row notice" @click="toCategory" v-if="couponInfo.content">{{couponInfo.content}}</view>
				<view class="notice_imgaes" v-else>
					<image src="../../../static/tishi.png"></image>
				</view>
			</view>
		</view>
		<view class="bottom-line-between"></view>
		<!-- 现金红包 -->
		<view class="red-packet margin" @click="toRedPacket">
			<view class="red-packet-header">
				<image src="/static/red-packet2.png" class="bg"></image>
				<view class="col tips-text">
					<text>{{couponTitleInfo.title}}</text>
					<text v-if="couponTitleInfo.title">抢现金红包</text>
					<!-- v-if="couponTitleInfo.title" -->
					<text v-else style="color: #fff;">本店暂时没有红包可抢哦~</text>
				</view>
			</view>
			<view class="col log" v-if="yesterdayLuckyInfo.member_id">
				<view class="row detail-item">
					<view class="avatar-view">
						<image :src="url+'/static/imgs/hat.png'" class="hat"></image>
						<image :src="yesterdayLuckyInfo.member_info.avatar" class="avatar"></image>
					</view>
					<view class="col detail-view">
						<text class="name">昨日锦鲤</text>
						<view class="detail">
							<text>{{yesterdayLuckyInfo.member_info.nickname}}</text>
							<text>抢到了</text>
							<text>{{yesterdayLuckyInfo.money}}元</text>
							<text v-if="yesterdayLuckyInfo.type==1">优惠券</text>
							<text v-else-if="yesterdayLuckyInfo.type==2">独家红包</text>
							<text v-else-if="yesterdayLuckyInfo.type==3">现金红包</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom-line-between"></view>
		<!-- 店长广播 -->
		<view class="col notice-view margin" v-if="noticeData.length>0">
			<view class="row title">
				<view class="row">
					<image :src="url+'/static/imgs/icon/notice.png'" class="icon"></image>
					<text>店长广播</text>
				</view>
			</view>
			<swiper :indicator-dots="false" class="swiper-notice" previous-margin="30rpx" next-margin="30rpx">
				<swiper-item class="row" v-for="(item,index) in noticeData" v-bind:key="index" @click="toNoticeDetail" :data-id="item.id">
					<view class="row notice">
						<view class="content text-ellipsis">
							<text>{{item.name}}</text>
							<!-- <text class="orange">送两份菜</text> -->
						</view>
						<image :src="url+'/static/imgs/more-black.png'" class="more-image"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="bottom-line-between" v-if="noticeData.length>0"></view>
		<!-- 会员福利 -->
		<view class="col welfare margin" v-if="giveGoodsData">
			<view class="row title">
				<view class="row">
					<image :src="url+'/static/imgs/icon/v.png'" class="icon" mode="aspectFill"></image>
					<text>会员福利</text>
				</view>
			</view>
			<view class="row detail">
				<image :src="giveGoodsData.goods_info.image"></image>
				<view class="col">
					<text>本月会员送菜啦~</text>
					<text>每人每月限领一次哦~</text>
				</view>
				<view class="to-more button" @click="toGiveMonth">查看更多</view>
			</view>
		</view>
		<view class="bottom-line-between" v-if="giveGoodsData"></view>
		<!-- 美食冠名 -->
		<view class="col naming-view margin">
			<view class="row title">
				<view class="row">
					<image :src="url+'/static/imgs/icon/naming.png'" class="icon"></image>
					<text>美食冠名</text>
				</view>
				<view class="button auto-left buttonauto_left" @click="toNamingList">争夺冠名</view>
			</view>

			<view class="row info">
				<view v-if="lastnamingData">恭喜{{lastnamingData.nickname}}冠名成功！【{{lastnamingData.gods_name}}】正式冠名为<text>【{{lastnamingData.naming}}<strong></strong>】</text>{{lastnamingData.gods_name}}
				</view>
				<view v-else>
					<text>0.1</text>元起 即可夺取属于你的冠名专属菜！
				</view>
				<image class="success" :src="url+'/static/imgs/nameing-success.png'" mode="widthFix"></image>
			</view>
		</view>
		<view @click.stop="changeMask" @touchmove.stop="catchtouchmove" class="mask" v-if="showMask"></view>
		<view class="user-number-mask" v-if="showMask">
			<view class="col number-main">
				<text class="number-title">选择人数</text>
				<view class="row mask-number-operate">
					<image :src="url+'/static/imgs/descreament-user.png'" class="descreament" @click="descreament"></image>
					<text class="number-text">{{peopleCount}}</text>
					<image :src="url+'/static/imgs/increament-user.png'" class="increament" @click="increament"></image>
				</view>
				<view class="mask-cancel row">
					<view @click="numberMaskCancel">取消</view>
					<view @click="numberMaskConfirm" class="theme-font-color">确定</view>
				</view>
			</view>
		</view>
		<view @click.stop="chnageFreeGoodsMask" @touchmove.stop="catchtouchmove" class="mask" v-if="freeGoodsMessgae.length>0&&showFreeGoodsMessage"></view>
		<view class="free-goods-mask" v-if="freeGoodsMessgae.length>0&&showFreeGoodsMessage">
			<view class="col free-goods-main">
				<view class="col free-goods-user-info">
					<image class="free-goods-avatar" :src="freeGoodsMessgae[0].avatar"></image>
					<text>{{freeGoodsMessgae[0].nickname}}</text>
				</view>
				<text class="free-goods-content">{{freeGoodsMessgae[0].content}}</text>
				<text class="free-goods-name">【{{freeGoodsMessgae[0].goods_name}}】</text>
				<view class="free-goods-cover">
					<image class="free-goods-bg" :src="url+'/static/imgs/free-goods-bg.png'"></image>
					<view class="row free-goods-image">
						<image :src="freeGoodsMessgae[0].image"></image>
					</view>
				</view>
				<view class="free-goods-button" @click="comfirmFreeGoods">朕知道了</view>
			</view>
		</view>
	</view>
</template>
<style>
	@import url("../../../common/css/store-info.css");

	.buttonauto_left {
		margin-right: 16rpx;
	}
	.notice_imgaes image{
		width:713rpx;
		height: 115rpx;
		display: block;
	}
</style>
<script>
	var app = getApp();
	import http from '../../../common/js/request.js';
	import common from '../../../common/js/function.js';

	export default {
		data() {
			return {
				store_id: app.globalData.storeId,
				url: app.globalData.baseImgUrl,
				storeInfo: [],
				noticeData: [],
				giveGoodsData: [],
				lastnamingData: '',
				couponInfo: [],
				yesterdayLuckyInfo: [],
				couponTitleInfo: [],
				showMask: false, //选择用餐人数的弹窗
				peopleCount: 1, //用餐人数
				choosePeopleCount: app.globalData.choosePeopleCount, //是否已选用餐人数
				freeGoodsMessgae: [], //免费餐品消息的弹窗(多条)
				showFreeGoodsMessage: true, //是否可以展示免费商品消息的弹窗
				gifts: {}, //送菜参数信息
			}
		},

		onLoad(options) {
			var that = this;
			app.globalData.init(() => {
				// options.q = 'https://store.qidian2018.com/index.php?mode=wx_qrcode&type=give_goods&send_id=5&goods_id=20&store_id=9'
				// options.q ='https://store.qidian2018.com/index.php?mode=wx_qrcode&type=give_goods&store_id=9&send_id=3&goods_id=20';
				if (options.q) {
					options.q = decodeURIComponent(options.q);
					console.log(options.q, "看得见款拉萨角度看")
					var q = common.urlFormatToJson(options.q);
					console.log(q);
					if (q.type == 'store_home') {
						//堂食桌面扫码
						that.storeId = q.store_id;
						that.deskId = q.desk_id;
						app.globalData.storeId = q.store_id;
						app.globalData.deskId = q.desk_id;
						that._load();
					} else if (q.type == 'give_goods') {
						//好友送菜
						console.log(q.share_id)
						that.storeId = q.store_id;
						app.globalData.storeId = q.store_id;
						that.gifts.send_id = q.send_id;
						that.gifts.goods_id = q.goods_id;
						that.gifts.share_id = q.share_id;
						that.receiveMemberGoodsGifts();
						that._load();
					}
				} else if (options.mode == 'wx_share') {
					that.storeId = options.store_id;
					app.globalData.storeId = options.store_id;
					that.gifts.send_id = options.send_id;
					that.gifts.goods_id = options.goods_id;
					that.gifts.share_id = options.share_id;
					that.receiveMemberGoodsGifts();
					that._load();
				}
			});
		},
		onShow() {
			var that = this;
			that._load();

		},
		onPullDownRefresh() {
			var that = this;
			that._load();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 400);

		},
		onHide() {
			uni.setStorageSync("timeLimit", {
				storeId: app.globalData.storeId,
				currentTime: Date.parse(new Date())
			})
		},
		methods: {
			_load() {
				var that = this;
				if (!app.globalData.storeId) return;
				app.globalData.init(() => {
					that.getStoreInfo();
					that.getNoticeData();
					that.getGiveGoodsData();
					that.getLastnamingData();
					that.getCouponInfo();
					that.getCouponTitleInfo();
					that.getYesterdayLuckyInfo();
					that.getFreeGoodsMessgae();
				});
			},
			//领取好友送的菜
			receiveMemberGoodsGifts() {
				var that = this;
				console.log(that.gifts, "参数")
				http.post('/api/v1/goods/receive_member_goods_gifts', {
					send_id: that.gifts.send_id,
					goods_id: that.gifts.goods_id,
					store_id: that.store_id,
					share_id: that.gifts.share_id
				}, res => {
					if (res.state == 1) {
						that.getFreeGoodsMessgae();
					} else {
						uni.showToast({
							title: res.datas,
							icon: 'none'
						});
					}
				});
			},
			//获取送菜回礼弹窗
			getFreeGoodsMessgae() {
				var that = this;
				http.post('/api/v1/store/get_member_no_read_goods_gifts', {
					store_id: that.store_id
				}, res => {
					that.freeGoodsMessgae = res.datas;
					console.log(that.freeGoodsMessgae)
				});
			},
			//店铺详情
			getStoreInfo() {
				var that = this;
				console.log('store_id:'+that.store_id);
				console.log('store_id->>>>>:'+app.globalData.storeId);
				
				http.post('/api/v1/store/store_detail', {
					store_id: app.globalData.storeId
				}, res => {
					console.log(112313123131313132);
					console.log(res.datas);
					that.storeInfo = res.datas;
					uni.setNavigationBarTitle({
						title: res.datas.store_name
					});
				});
			},
			//收藏/取消收藏店铺
			collect() {
				var that = this;
				uni.showLoading();
				common.verifyAuth(() => {
					http.post('/api/v1/store/make_col', {
						store_id: that.store_id
					}, res => {
						if (res.state == 1) {
							uni.hideLoading();
							uni.showToast({
								title: that.storeInfo.my_is_col == 0 ? '已收藏~' : '已取消收藏~',
								icon: 'none'
							});
							that.storeInfo.my_is_col = that.storeInfo.my_is_col == 1 ? 0 : 1;
						}

					});
				});
			},
			//优惠券信息
			getCouponInfo() {
				var that = this;
				http.post('/api/v1/store/get_last_something', {
					store_id: that.store_id
				}, res => {
					that.couponInfo = res.datas;
				});
			},
			//现金红包信息
			getCouponTitleInfo() {
				var that = this;
				http.post('/api/v1/coupon/coupon_title', {
					store_id: that.store_id,
				}, res => {
					that.couponTitleInfo = res.datas;
				});
			},
			//获取昨日锦鲤
			getYesterdayLuckyInfo() {
				var that = this;
				http.post('/api/v1/coupon/get_yesterday_lucky', {
					store_id: that.store_id
				}, res => {
					that.yesterdayLuckyInfo = res.datas;
				});
			},
			//店长广播
			getNoticeData() {
				var that = this;
				http.post('/api/v1/store/store_article_list', {
					store_id: that.store_id,
					page: 1
				}, res => {
					that.noticeData = res.datas;
				});
			},
			//本月送菜信息
			getGiveGoodsData() {
				var that = this;
				http.post('/api/v1/store/get_gifts_goods_info', {
					store_id: that.store_id,
				}, res => {
					that.giveGoodsData = res.datas;
				});
			},
			//最后冠名信息
			getLastnamingData() {
				var that = this;
				http.post('/api/v1/goods/get_last_naming', {
					store_id: that.store_id,
				}, res => {
					that.lastnamingData = res.datas;
				});

			},
			//地图查看
			openLocation() {
				var that = this;
				uni.openLocation({
					latitude: Number(that.storeInfo.latitude),
					longitude: Number(that.storeInfo.longitude),
					name: that.storeInfo.store_name
				})
			},
			//拨打电话
			makePhoneCall() {
				var that = this;
				uni.makePhoneCall({
					phoneNumber: that.storeInfo.mobile
				})
			},
			//跳转至店铺详情
			toStoreDetail() {
				var that = this;
				uni.navigateTo({
					url: '/pages/store/detail/detail'
				});
			},
			//跳转至点餐（分类页面）
			toCategory() {
				var that = this;
				common.verifyAuth(() => {
					if (that.choosePeopleCount == false) {
						that.changeMask();
					} else {
						uni.switchTab({
							url: '/pages/store/category/category'
						})
					}
				});

			},
			//跳转至打赏页面
			toPlayTour(e) {
				var that = this;
				common.verifyAuth(() => {
					console.log(e)
					uni.navigateTo({
						url: '/pages/store/playTour/playTour'
					})
				});

			},
			//跳转至优惠券列表
			toCouponList() {
				var that = this;
				common.verifyAuth(() => {
					uni.navigateTo({
						url: '/pages/store/couponList/couponList'
					})
				});

			},
			//跳转至领红包页面
			toRedPacket() {
				var that = this;
				common.verifyAuth(() => {
					uni.navigateTo({
						url: '/pages/store/redPacket/redPacket'
					})
				});

			},
			//跳转至每月送菜
			toGiveMonth() {
				var that = this;
				common.verifyAuth(() => {
					uni.navigateTo({
						url: '/pages/store/give/month/month'
					})
				});

			},
			//跳转至我要冠名
			toNamingList() {
				var that = this;
				common.verifyAuth(() => {
					uni.navigateTo({
						url: '/pages/store/naming/list/list'
					})
				});

			},
			toNoticeDetail(e) {
				var that = this;
				uni.navigateTo({
					url: '/pages/store/notice/detail?id=' + e.currentTarget.dataset.id
				});
			},
			//遮罩状态切换
			changeMask() {
				this.showMask = !this.showMask;
			},

			//免费餐品-朕知道了
			comfirmFreeGoods() {
				var that = this;
				uni.showLoading();
				http.post('/api/v1/store/make_member_read_goods_gifts', {
					store_id: that.store_id,
					gifts_id: that.freeGoodsMessgae[0].gifts_id
				}, res => {
					if (res.state == 1) {
						console.log(res)
						uni.hideLoading();
						that.freeGoodsMessgae.splice(0, 1);
						that.freeGoodsMessgae = that.freeGoodsMessgae;
						that.showFreeGoodsMessage = false;
						setTimeout(function() {
							that.showFreeGoodsMessage = true;
						}, 200);
					} else {
						uni.showToast({
							title: res.datas,
							icon: 'none'
						});
					}

				});
			},
			//阻止遮罩滑动
			catchtouchmove() {
				return false;
			},
			//关闭选择人数的弹窗
			numberMaskCancel() {
				var that = this;
				that.changeMask();
			},
			//用餐人数确定
			numberMaskConfirm() {
				var that = this;
				that.changeMask();
				http.post('/api/v1/cart/add_auto_cart', {
					store_id: that.store_id,
					user_num: that.peopleCount
				}, res => {
					if (res.state == 1) {
						app.globalData.choosePeopleCount = true;
						that.choosePeopleCount = true;
						uni.switchTab({
							url: '/pages/store/category/category'
						});
					} else {
						uni.showToast({
							title: res.datas,
							icon: 'none'
						});
					}
				});
			},
			descreament() {
				if (this.peopleCount == 1) {
					uni.showToast({
						title: '用餐人数最少为一位~',
						icon: 'none'
					});
					return;
				}
				this.peopleCount -= 1;
			},
			increament() {
				this.peopleCount += 1;
			},
			onShareAppMessage() {

			}
		}
	}
</script>
